package com.wans.order.controller;

import com.wans.order.bean.Order;
import com.wans.order.properties.OrderProperties;
import com.wans.order.service.OrderBuyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RefreshScope // 自动刷新Nacos配置
@RestController
public class OrderBuy {

    @Autowired
    private OrderBuyService orderBuyService;
    @Autowired
    private OrderProperties orderProperties;

    @Value("${order.timeout}")
    private String timeOut;
    @Value("${order.auto-confirm}")
    private String autoConfirm;


    /**
     * 创建订单
     *
     * @param userId    用户id
     * @param productId 商品id
     * @return 订单信息
     */
    @GetMapping("/order/create")
    public Order createOrder(Long userId, Long productId) {
        return orderBuyService.createOrder(userId, productId);
    }

    /**
     * 获取配置信息
     *
     * @return 配置信息
     */
    @GetMapping("/order/getProperties")
    public String getProperties() {
        return "server-product-info -> " + timeOut + " - " + autoConfirm;
    }

    /**
     * 获取配置信息 spring 无感刷新Nacos配置
     *
     * @return 配置信息
     */
    @GetMapping("/order/getProperties2")
    public String getProperties2() {
        return "server-product-info -> "
                + orderProperties.getTimeout() + " - "
                + orderProperties.getAutoConfirm() + " - "
                + orderProperties.getNamespace();
    }
}
